/*
 * Copyright (C) 2011 CrystalRange
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.crystalrange.web;

import com.googlecode.genericdao.search.Search;
import java.util.List;
import java.util.Set;
import org.crystalrange.BaseController;
import org.crystalrange.model.Script;
import org.crystalrange.service.ScriptService;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

public class ScriptListController extends BaseController {

    protected ScriptService scriptService;
    protected Script selectedScript;
    protected Listbox listboxScript;
    protected Search searchScript;
    protected List<Script> scripts;

    public ScriptService getScriptService() {
        return this.scriptService;
    }

    public void setScriptService(ScriptService scriptService) {
        this.scriptService = scriptService;
    }

    public Script getSelectedScript() {
        return this.selectedScript;
    }

    public void setSelectedScript(Script selectedScript) {
        this.selectedScript = selectedScript;
    }

    public List<Script> getScripts() {
        return this.scripts;
    }

    @Override
    public void doAfterCompose(Component component) throws Exception {
        super.doAfterCompose(component);

        this.searchScript = new Search(Script.class);
        this.searchScript.setMaxResults(100);
        this.searchScript.addFetch("createdBy");
        this.searchScript.addFetch("updatedBy");
        this.scripts = this.scriptService.search(this.searchScript);

        component.setAttribute("controller", this, false);
    }

    public void onClick$menuitemScriptCreate(Event event) throws Exception {
        this.scriptCreate();
    }

    public void onClick$menuitemScriptEdit(Event event) throws Exception {
        this.scriptEdit();
    }

    public void onClick$menuitemScriptDelete(Event event) throws Exception {
        this.scriptDelete();
    }

    public void onClick$menuitemScriptSearch(Event event) throws Exception {
        this.scriptSearch();
    }

    public void onClick$menuitemScriptRefresh(Event event) throws Exception {
        this.scriptRefresh();
    }

    public void onClick$toolbarbuttonScriptCreate(Event event) throws Exception {
        this.scriptCreate();
    }

    public void onClick$toolbarbuttonScriptEdit(Event event) throws Exception {
        this.scriptEdit();
    }

    public void onClick$toolbarbuttonScriptDelete(Event event) throws Exception {
        this.scriptDelete();
    }

    public void onClick$toolbarbuttonScriptSearch(Event event) throws Exception {
        this.scriptSearch();
    }

    public void onClick$toolbarbuttonScriptRefresh(Event event) throws Exception {
        this.scriptRefresh();
    }

    public void onDoubleClickListItem(Listitem listitem) throws Exception {
        this.scriptEdit();
    }
    
    private void scriptCreate() throws Exception {
        this.execution.sendRedirect("~./zul/html/script.zul");
    }

    private void scriptEdit() throws Exception {
        if (this.selectedScript != null) {
            this.execution.sendRedirect("~./zul/html/script.zul?Id=" + this.selectedScript.getId().toString());
        }
    }

    private void scriptDelete() throws Exception {
        Set<Listitem> selectedItems = this.listboxScript.getSelectedItems();

        if (selectedItems.size() > 0) {
            if (Messagebox.YES == Messagebox.show("Delete these records?", "Delete?",
                    Messagebox.YES | Messagebox.NO, Messagebox.QUESTION)) {
                for (Listitem selectedItem : selectedItems) {
                    Script script = (Script) selectedItem.getValue();
                    this.scriptService.remove(script);
                }
            }
        } else {
            this.message.setText("Please select at least one item!");
            this.message.setIcon("/img/exclamation.png");
        }

        this.scripts = this.scriptService.search(this.searchScript);
        this.binder.loadAll();
    }

    private void scriptSearch() throws Exception {
        this.execution.setAttribute("searchScript", this.searchScript);

        Window window = (Window) this.execution.createComponents("~./zul/html/scriptSearch.zul", null, null);
        window.setParent(self);
        window.doModal();

        if (this.execution.getAttribute("result") == "ok") {
            this.searchScript = (Search) this.execution.getAttribute("searchScript");
        }

        this.scripts = this.scriptService.search(this.searchScript);
        this.binder.loadAll();
    }

    private void scriptRefresh() {
        this.scripts = this.scriptService.search(this.searchScript);
        this.binder.loadAll();
    }
}
